package com.zk.transaction.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    private static  DataSource DATA_SOURCE = null ;

    public static void init() throws Exception {
        Properties prop = new Properties();
        prop.put("driverClassName","com.mysql.jdbc.Driver");
        prop.put("url","jdbc:mysql://127.0.0.1/mp?useSSL=false&useServerPrepStmts=true&allowPublicKeyRetrieval=true");
        prop.put("username","root");
        prop.put("password","root");
        DATA_SOURCE = DruidDataSourceFactory.createDataSource(prop);
    }
    /**
     * 获得连接的方法
     */
    public static Connection getConnection(){
        Connection conn = null;
        try {
            if (null == DATA_SOURCE){
                init();
            }
            conn = DATA_SOURCE.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    public static DataSource getDataSource() throws Exception {
        if (null == DATA_SOURCE){
            init();
        }
        return DATA_SOURCE;
    }


}
